import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;

class MyThread2 implements Runnable {
    @Override
    public void run() {

    }
}

class Mythread implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("-----come in call-----");
        Thread.sleep(3);
        System.out.println("--come out call----");
        return 1024;
    }
}

/**
 1 get是阻塞方法，方法一般放在最后一行
 */
public class CallableDemo {
    /**
     * futureTask只允许一次，不管几个线程
     */
    @Test
    public void testTwoThread() {
        RunnableFuture<Integer> futureTask = new FutureTask(new Mythread());   // 适配器模式
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        RunnableFuture<Integer> futureTask = new FutureTask(new Mythread());
        new Thread(futureTask, "A").start();
        try {
//            Integer result = futureTask.get();
            Boolean boo = futureTask.isDone();
//            System.out.println(result);
            System.out.println(boo);
            if (boo == false) {
                futureTask.cancel(false);
                System.out.println(futureTask.isDone());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
